package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Operator;
import edu.unindra.raport.dao.impl.OperatorImpl;

import javax.swing.JPasswordField;

public class FormDetailMenuOperatorEdit extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JButton btnUbah = null;
	private JButton btnBatal = null;

	private FormDetailMenuOperator parent;

	private JLabel lblId;
	private JTextField txtId;

	private JLabel lblNama;
	private JLabel lblKontak;
	private JLabel lblAlamat;
	private JTextArea textArea;
	private JTextField txtNama;
	private JTextField txtKontak;

	private OperatorImpl opImpl = null;

	private JLabel lblPassword;
	private JPasswordField txtPassword;

	public FormDetailMenuOperatorEdit(FormDetailMenuOperator parent) {
		this.parent = parent;

		opImpl = new OperatorImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Ubah Data Operator ");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 276);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblId = new GridBagConstraints();
			gbc_lblId.gridx = 1;
			gbc_lblId.anchor = GridBagConstraints.WEST;
			gbc_lblId.insets = new Insets(2, 2, 5, 5);
			gbc_lblId.weightx = 0.0D;
			gbc_lblId.gridy = 0;
			this.lblId = new JLabel();
			lblId.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblId.setText("Id Operator");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
					1.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtId = new GridBagConstraints();
			gbc_txtId.gridwidth = 3;
			gbc_txtId.insets = new Insets(0, 0, 5, 5);
			gbc_txtId.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtId.gridx = 3;
			gbc_txtId.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtId);
			this.jContentPane.add(this.lblId, gbc_lblId);
			GridBagConstraints gbc_lblPassword = new GridBagConstraints();
			gbc_lblPassword.anchor = GridBagConstraints.WEST;
			gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
			gbc_lblPassword.gridx = 1;
			gbc_lblPassword.gridy = 1;
			jContentPane.add(getLblPassword(), gbc_lblPassword);
			GridBagConstraints gbc_txtPassword = new GridBagConstraints();
			gbc_txtPassword.gridwidth = 3;
			gbc_txtPassword.insets = new Insets(0, 0, 5, 5);
			gbc_txtPassword.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtPassword.gridx = 3;
			gbc_txtPassword.gridy = 1;
			jContentPane.add(getTxtPassword(), gbc_txtPassword);
			GridBagConstraints gbc_lblNama = new GridBagConstraints();
			gbc_lblNama.anchor = GridBagConstraints.WEST;
			gbc_lblNama.insets = new Insets(0, 0, 5, 5);
			gbc_lblNama.gridx = 1;
			gbc_lblNama.gridy = 2;
			jContentPane.add(getLblNama(), gbc_lblNama);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 2;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblKontak = new GridBagConstraints();
			gbc_lblKontak.anchor = GridBagConstraints.WEST;
			gbc_lblKontak.insets = new Insets(0, 0, 5, 5);
			gbc_lblKontak.gridx = 1;
			gbc_lblKontak.gridy = 3;
			jContentPane.add(getLblKontak(), gbc_lblKontak);
			GridBagConstraints gbc_txtKontak = new GridBagConstraints();
			gbc_txtKontak.gridwidth = 3;
			gbc_txtKontak.insets = new Insets(0, 0, 5, 5);
			gbc_txtKontak.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtKontak.gridx = 3;
			gbc_txtKontak.gridy = 3;
			jContentPane.add(getTxtKontak(), gbc_txtKontak);
			GridBagConstraints gbc_lblAlamat = new GridBagConstraints();
			gbc_lblAlamat.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblAlamat.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlamat.gridx = 1;
			gbc_lblAlamat.gridy = 4;
			jContentPane.add(getLblAlamat(), gbc_lblAlamat);
			GridBagConstraints gbc_textArea = new GridBagConstraints();
			gbc_textArea.gridwidth = 3;
			gbc_textArea.insets = new Insets(0, 0, 5, 5);
			gbc_textArea.fill = GridBagConstraints.BOTH;
			gbc_textArea.gridx = 3;
			gbc_textArea.gridy = 4;
			jContentPane.add(getTextArea(), gbc_textArea);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 5;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnUbah = new GridBagConstraints();
			gbc_btnUbah.gridx = 6;
			gbc_btnUbah.insets = new Insets(2, 2, 2, 0);
			gbc_btnUbah.anchor = 13;
			gbc_btnUbah.gridy = 5;
			this.jContentPane.add(getBtnUbah(), gbc_btnUbah);
		}
		return this.jContentPane;
	}

	private JButton getBtnUbah() {
		if (this.btnUbah == null) {
			this.btnUbah = new JButton();
			this.btnUbah.setText("Ubah");
			this.btnUbah.setToolTipText("Insert");
			this.btnUbah.addActionListener(new ActionListener() {
				@SuppressWarnings("deprecation")
				public void actionPerformed(ActionEvent e) {

					int retVal = 0;
					Operator em = new Operator();

					em.setId(txtId.getText());
					em.setPassword(txtPassword.getText());
					em.setName(txtNama.getText());
					em.setContact(txtKontak.getText());
					em.setAddress(textArea.getText());

					try {
						retVal = opImpl.update(em);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

					if (retVal > -1) {
						JOptionPane.showMessageDialog(
								FormDetailMenuOperatorEdit.this,
								"Proses update data karyawan  berhasil");
						int i = parent.getTable().convertRowIndexToModel(
								parent.getTable().getSelectedRow());

						parent.getModelTable().setOperatorAt(em, i);

						FormDetailMenuOperatorEdit.this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuOperatorEdit.this,
								"Proses update data karyawan  berhasil");
					}
				}
			});
		}
		return this.btnUbah;
	}

	JTextField getTxtCardNumber() {
		if (txtId == null) {
			txtId = new JTextField();
			txtId.setColumns(10);

			txtId.setEnabled(false);
		}
		return txtId;
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.setToolTipText("Insert");
			btnBatal.setText("Batal");
			btnBatal.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					FormDetailMenuOperatorEdit.this.setVisible(false);
				}
			});
		}
		return btnBatal;
	}

	private JLabel getLblNama() {
		if (lblNama == null) {
			lblNama = new JLabel();
			lblNama.setText("Nama Operator");
		}
		return lblNama;
	}

	private JLabel getLblKontak() {
		if (lblKontak == null) {
			lblKontak = new JLabel();
			lblKontak.setText("Kontak");
		}
		return lblKontak;
	}

	private JLabel getLblAlamat() {
		if (lblAlamat == null) {
			lblAlamat = new JLabel();
			lblAlamat.setVerticalAlignment(SwingConstants.TOP);
			lblAlamat.setText("Alamat");
		}
		return lblAlamat;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	private JTextField getTxtKontak() {
		if (txtKontak == null) {
			txtKontak = new JTextField();
			txtKontak.setColumns(10);
		}
		return txtKontak;
	}

	public void setOperator(Operator e) {
		if (e == null) {
			throw new IllegalArgumentException();
		}

		txtId.setText(e.getId());
		txtPassword.setText(e.getPassword());
		txtNama.setText(e.getName());
		txtKontak.setText(e.getContact());
		textArea.setText(e.getAddress());
	}

	private JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel();
			lblPassword.setText("Password Operator");
			lblPassword.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblPassword;
	}

	private JPasswordField getTxtPassword() {
		if (txtPassword == null) {
			txtPassword = new JPasswordField();
		}
		return txtPassword;
	}
}